package java44th.group.controller.admin.role;

import java44th.group.bean.admin.bo.role.CreateBo;
import java44th.group.bean.admin.bo.role.PermissionsBo;
import java44th.group.bean.json.DataDTO;
import java44th.group.bean.po.MarketRole;
import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.bean.admin.vo.role.OptionsVo;
import java44th.group.bean.admin.vo.role.CreateVo;
import java44th.group.bean.admin.vo.role.RoleDateVo;
import java44th.group.service.admin.role.MarketRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Aurhor: 王可龙
 * @DATE: 2022/10/11 22:34
 * @Package:java44th.group.controller.admin.role
 * @Description:
 * @Version 1.0
 */
@RestController
@RequestMapping("admin/role")
public class OptionsController {

    @Autowired
    MarketRoleService marketRoleService;
    @RequiresPermissions("*")
    @RequestMapping("list")
    public BaseRespVo list(Integer page,Integer limit,String name,String sort,String order){
        RoleDateVo list = marketRoleService.list(page, limit,name, sort, order);
        return BaseRespVo.ok(list);
    }


    @RequestMapping("create")
    public BaseRespVo create(@RequestBody CreateBo createBo){
        System.out.println(createBo);
        CreateVo createVo=marketRoleService.create(createBo);
        if (createVo==null){
            return BaseRespVo.roleCreate();
        }
        return BaseRespVo.ok(createBo);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketRole marketRole){
        System.out.println(marketRole);
        Integer count = marketRoleService.update(marketRole);
        if (count==1){
            return BaseRespVo.error(502,"系统内部错误");
        }
        return BaseRespVo.ok(null);
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketRole marketRole){
        System.out.println(marketRole);
        marketRoleService.delete(marketRole);
        return BaseRespVo.ok(null);
    }

    @RequestMapping("options")
    public BaseRespVo<OptionsVo> options(){
        OptionsVo optionsVo= marketRoleService.options();
        return BaseRespVo.ok(optionsVo);
    }


    @RequestMapping(value = "permissions",method = RequestMethod.GET)
    public BaseRespVo<DataDTO> permissions(Integer roleId){
        DataDTO dataDTO=marketRoleService.permissions(roleId);
        return BaseRespVo.ok(dataDTO);
    }

    @RequestMapping(value = "permissions",method = RequestMethod.POST)
    public BaseRespVo<DataDTO> permissions(@RequestBody PermissionsBo permissions){
        DataDTO dataDTO=marketRoleService.permissions2(permissions);
        return BaseRespVo.ok(dataDTO);
    }
}
